package com.xht.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xht.domain.ResponseResult;
import com.xht.domain.dto.TagListDto;
import com.xht.domain.entity.Tag;
import com.xht.enums.AppHttpCodeEnum;
import com.xht.exception.SystemExceptin;
import com.xht.mapper.TagMapper;
import com.xht.service.TagService;
import com.xht.utils.BeanCopyUtils;
import com.xht.vo.PageVo;
import com.xht.vo.TagVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 标签(Tag)表服务实现类
 *
 * @author makejava
 * @since 2025-03-04 17:08:48
 */
@Service("tagService")
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    private final TagMapper tagMapper;

    public TagServiceImpl(TagMapper tagMapper) {
        this.tagMapper = tagMapper;
    }

    @Override
    public ResponseResult<PageVo> pageTagList(Integer pageNum, Integer pageSize, TagListDto tagListDto) {
        //分页查询
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(tagListDto.getName()), Tag::getName, tagListDto.getName());

        queryWrapper.like(StringUtils.hasText(tagListDto.getRemark()), Tag::getRemark, tagListDto.getRemark());


        Page<Tag> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page, queryWrapper);

        //封装数据
        PageVo pageVo = new PageVo(page.getRecords(), page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    /**
     * 添加 标签
     *
     * @param name
     * @param remark
     * @return
     */
    @Override
    public ResponseResult addTag(String name, String remark) {
        //对 新增的 name和remark进行 判空
        if (!StringUtils.hasText(name)) {
            throw new SystemExceptin(AppHttpCodeEnum.TAG_NAME_NOT_NULL);
        }
        if (!StringUtils.hasText(remark)) {
            throw new SystemExceptin(AppHttpCodeEnum.TAG_REMARK_NOT_NULL);
        }

        //对新增的标签进行查重判断,如果标签中已经存在,则不添加
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Tag::getName, name);
        int count = this.count(queryWrapper);
        //如果查询 条数 大于0,证明数据中已经存在
        if (count > 0) {
            throw new SystemExceptin(AppHttpCodeEnum.TAG_NAME_EXIST);
        }

        Tag tag = new Tag();
        tag.setName(name);
        tag.setRemark(remark);
        save(tag);

        return ResponseResult.okResult();
    }

    /**
     * 删除 单个 标签
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteTag(Integer id) {
//        Tag tag = getById(id);
        //将该标签的逻辑删除标志更改为1  ,表示被删除了

        //小坑 修改最好使用 LambdaUpdateWrapper
//        Tag tag = getById(id);
//        tag.setName("test");
        update(new LambdaUpdateWrapper<Tag>().eq(Tag::getId, id).set(Tag::getDelFlag, 1));
//        updateById(tag);
//        Tag tag = new Tag();
//        tag.setId(Long.valueOf(id));
//        tag.setDelFlag(1);
//        baseMapper.updateById(tag);
//        LambdaUpdateWrapper<Tag> updateWrapper = Wrappers.lambdaUpdate(Tag.class)
//                .eq(Tag::getId, id)
//                .set(Tag::getDelFlag, 1);
//        baseMapper.update(tag,updateWrapper);
        //boolean b = updateById(tag);
        return ResponseResult.okResult();
    }

    /**
     * 获取 标签 信息
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult getTag(Integer id) {
        //根据 id查 标签
        Tag tag = getById(id);
        TagVo tagVo = BeanCopyUtils.copyBean(tag, TagVo.class);

        return ResponseResult.okResult(tagVo);
    }

    @Override
    public ResponseResult fixTag(Tag tag) {
        LambdaUpdateWrapper<Tag> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Tag::getId, tag.getId())
                .set(Tag::getName, tag.getName())
                .set(Tag::getRemark, tag.getRemark());
        update(updateWrapper);
        return ResponseResult.okResult();
    }

    /**
     * 写博文模块-展示所有标签
     *
     * @return
     */
    @Override
    public ResponseResult<TagVo> listAllTag() {
        //查询 所有未被删除的 tag
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Tag::getId, Tag::getName);
        List<Tag> list = list(queryWrapper);

        //封装数据返回
        List<TagVo> tagVos = BeanCopyUtils.copyBeanList(list, TagVo.class);

        return ResponseResult.okResult(tagVos);
    }
}

